<template name="loginusers">
    <div class="login">
        <el-form :model="loginForm" :rules="loginRules" class="container" ref="loginRef">
            <el-form-item>
                <div class="avatar">
                    <img src="../assets/avatar.jpg" alt="头像">
                </div>
            </el-form-item>
            <el-form-item prop="username">
                <el-input v-model="loginForm.username" prefix-icon="iconfont iconyonghu"></el-input>
            </el-form-item>
            <el-form-item prop="password">
                <el-input v-model="loginForm.password" prefix-icon="iconfont iconyuechi_o" type="password" @keyup.enter.native="handleLogin"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" class="login-btn" @click="handleLogin">登录</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>
<script>
import {Meteor} from 'meteor/meteor';
import {LoginUsers,UserLog} from '/imports/collections/collection'
export default {
    data () {
        return {
            loginForm: {
                // 定义form表单需要的数据
                username: '',
                password: ''
            },
            loginRules: {
                username: [
                { required: true, message: '请输入用户名', trigger: 'blur' }
                ],
                password: [
                { required: true, message: '请输入密码', trigger: 'blur' }
                ]
            }
        }
    },
    mounted () {
    },
    methods: {
        handleLogin () {
            // 进行js校验
            this.$refs.loginRef.validate(isok=>{
                if(isok){
                     // 判断输入的用户名和密码是否正确
                    var status=0;
                    for(var i=0;i<LoginUsers.find().count();i++){
                        if(LoginUsers.find({}).fetch()[i].username==this.loginForm.username && LoginUsers.find({}).fetch()[i].password===this.loginForm.password){
                            status=1
                        }else{
                        }
                    }

                    if(status==1){
                        // 将登录成功的用户名、密码保存在本地
                            localStorage.setItem('username',this.loginForm.username);
                            localStorage.setItem('password',this.loginForm.password);
                            // 保存登录的状态
                            localStorage.setItem('isLogin',true)
                            // 用户日志信息
                            UserLog.insert({
                                'username':this.loginForm.username,
                                'loginState':'登录成功',
                                // 'loginIp':'192.168.3.5',
                                'loginTime':this.dateFormat(Date.now())
                            })
                            //   跳转到首页
                            this.$router.push({
                                name:'welcome'
                            })
                    }else{
                        this.$message.error('登录失败!');
                    }
                }else{
                    this.$message.error('请输入正确的信息');
                    return false;
                }
            })
       
        },
        // 封装时间戳的转换
      dateFormat(time){
         // 时间戳转换 
                let date = new Date(time + 8 * 3600000);
                let str = date.toISOString().replace('T',' ');
                return str.substr(0, str.lastIndexOf('.'));
      }
    }
}
</script>
<style scoped>
    .login {
        position: fixed;
        width: 100%;
        height: 100%;
        background-color: #2f4050;
}

.login .container {
        position: absolute;
        left: 0;
        right: 0;
        width: 400px;
        padding: 0px 40px 15px 40px;
        margin: 150px auto;
        background: white;
}
.login  .container .avatar {
        position: relative;
        left: 50%;
        width: 120px;
        height: 120px;
        margin-left: -60px;
        margin-top: -60px;
        box-sizing: border-box;
        border-radius: 50%;
        border: 10px solid #fff;
        box-shadow: 0 1px 5px #ccc;
        overflow: hidden;
    }
    .login  .container .login-btn {
        width: 100%;
    }
</style>
